

function Close(){
    if(GameUI.CustomUIConfig().isAbilitiesPoolOpened===true){
        GameEvents.SendEventClientSide("toggle_abilities_pool",{})
    }
}

(function(Global /*, context */) {

    Global.isAbilitiesPoolOpened = false;
    var rowColCount=10
    var abilitiesPoolELE=$("#AbilitiesPool");

    function RefreshAbilitiesPool(data){
        if(data){
            // $.Msg("RefreshAbilitiesPool     ",data)
            var row=abilitiesPoolELE.FindChildTraverse( "row0" )
            if(!row){
                for(var i=0;i<Math.ceil(Global.GameConfig.abilitiesShopCount/rowColCount);i++){
                   row= $.CreatePanel( "Panel", abilitiesPoolELE, "row"+i );
                    row.SetHasClass("AbilitiesPoolRow",true)
                    var item
                   for(var j=1;j<=(rowColCount);j++){
                       item = $.CreatePanel( "Panel", row, "item"+(j+i*rowColCount) );
                       item.BLoadLayout( "file://{resources}/layout/custom_game/abilities_pool_item.xml", false, false );
                   }
                }
                row=abilitiesPoolELE.FindChildTraverse( "row0" )
            }
            for(var index in data){
                // $.Msg(data[index])
                var item = abilitiesPoolELE.FindChildTraverse("item" + index)
                // $.Msg(item);
                var itemData = data[index];
                switch (itemData.abilityType) {
                    case "DOTA_ABILITY_TYPE_ULTIMATE":
                        item.SetHasClass("BasItem", false)
                        item.SetHasClass("AttrItem", false)
                        item.SetHasClass("UltItem", true)
                        break;
                    case "DOTA_ABILITY_TYPE_ATTRIBUTES":
                        item.SetHasClass("BasItem", false)
                        item.SetHasClass("UltItem", false)
                        item.SetHasClass("AttrItem", true)
                        break;
                    default:
                        item.SetHasClass("BasItem", true)
                        item.SetHasClass("UltItem", false)
                        item.SetHasClass("AttrItem", false)
                        break;
                }

                item = item.FindChildTraverse("AbilitiesPoolItemRoot")
                item.SetItemData(itemData)
            }
        }
    }

    function OnToggleAbilitiesPool() {
        abilitiesPoolELE.style.visibility="visible"
        if(Global.Panel.topBarAbilitiesPool){
            Global.Panel.topBarAbilitiesPool.SetHasClass("Selected",!Global.isAbilitiesPoolOpened)
        }
        if(Global.isAbilitiesPoolOpened===true){
            Game.EmitSound("Shop.PanelDown")
            Global.isAbilitiesPoolOpened = false;
            abilitiesPoolELE.style['position'] = '400px -450px 0px';
            abilitiesPoolELE.style['transform'] = 'scale3d( 0.001,0.001,0.001)';
        }
        else {
            Game.EmitSound("Shop.PanelUp")
            Global.isAbilitiesPoolOpened = true;
            abilitiesPoolELE.style['position'] = '0px 0px 0px';
            abilitiesPoolELE.style['transform'] = 'scale3d( 1,1,1)';
        }
    }

    function OnAbilitiesPoolChanged(table, key, data) {
            RefreshAbilitiesPool(data)
    }





    GameEvents.Subscribe( "toggle_abilities_pool", OnToggleAbilitiesPool );
    CustomNetTables.SubscribeNetTableListener("abilities_pool", OnAbilitiesPoolChanged);
    RefreshAbilitiesPool(CustomNetTables.GetTableValue("abilities_pool", "all"))

})(GameUI.CustomUIConfig(), this);